﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Windows.Forms;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using XCI.Windows;
using XCI.Helper;

namespace XCI.Setting.Controls
{
    public partial class SettingTreeBands : BaseUserControl
    {
        private TreeList _treeList;
        public SettingTreeBands()
        {
            InitializeComponent();
        }

        public void Init(TreeList treeList)
        {
            this._treeList = treeList;
            if (treeList == null) return;
            LoadData();
            SetStatus();
        }

        protected void LoadData()
        {
            listBoxControl1.Items.BeginUpdate();
            listBoxControl1.Items.Clear();

            for (var index = 0; index < _treeList.Bands.Count; index++)
            {
                var item = _treeList.Bands[index];
                var name = $"列分组 - {index + 1}";
                if (!string.IsNullOrWhiteSpace(item.Caption))
                {
                    name = item.Caption;
                }
                listBoxControl1.Items.Add(name);
            }
            listBoxControl1.Items.EndUpdate();
        }

        private void SetStatus()
        {
            var index = listBoxControl1.SelectedIndex;
            btnDelete.Enabled = index > -1;
        }

        private void listBoxControl1_SelectedValueChanged(object sender, EventArgs e)
        {
            SetStatus();
            var index = listBoxControl1.SelectedIndex;
            if (index == -1) return;

            var obj = _treeList.Bands[index];
            xciPropertyGrid1.SelectedObject = obj;
            xciPropertyGrid2.SelectedObject = obj.OptionsBand;
            xciPropertyGrid3.SelectedObject = obj.AppearanceHeader;
        }

        private void xciPropertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.Label == null || !e.ChangedItem.Label.Equals("Caption")) return;
            LoadData();
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bcount = _treeList.Bands.Count;
            var bindex = bcount + 1;
            var band = new TreeListBand { Caption = "treeBand" + bindex, Name = "treeBand" + bindex};

            _treeList.Bands.Add(band);
            LoadData();
            listBoxControl1.SelectedIndex = bcount;
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中项吗？") == DialogResult.Yes)
            {
                _treeList.Bands.RemoveAt(listBoxControl1.SelectedIndex);
                LoadData();
            }
        }
    }
}
